package com.run.cat.common.util;


import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestUtil {
	/**
	 * 获取request对象
	 *
	 * @return request
	 */
	public static HttpServletRequest getRequest() {
		try {
			return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		} catch (Exception e) {
			return null;
		}
	}

	public static HttpServletResponse getResponse() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletResponse response = attributes.getResponse();
		return response;
	}

	/**
	 * 项目的真实路径
	 *
	 * @return String
	 */
	public static String getPjoPath() {
		try {
			return // 项目的真实路径
					StringUtils.replace(getRequest().getServletContext().getContextPath(), "/", "\\");
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获取客户端请求的路径名，如：/object/delObject
	 *
	 * @return String
	 */
	public static String getServletPath() {
		try {
			return // 项目的真实路径
					getRequest().getServletPath();
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获取服务器地址，如：localhost
	 *
	 * @return String
	 */
	public static String getServerName() {
		try {
			return // 项目的真实路径
					getRequest().getServerName();
		} catch (Exception e) {
			return null;
		}
	}



	/**
	 * 获用户地址，如：127.0.0.1
	 *
	 * @return String
	 */
	public static String getRemoteAddr() {
		try {
			String remoteAddr = getRequest().getHeader("X-Real-IP");
			if (StringUtils.isNotBlank(remoteAddr)) {
				remoteAddr = getRequest().getHeader("X-Forwarded-For");
			} else if (StringUtils.isNotBlank(remoteAddr)) {
				remoteAddr = getRequest().getHeader("Proxy-Client-IP");
			} else if (StringUtils.isNotBlank(remoteAddr)) {
				remoteAddr = getRequest().getHeader("WL-Proxy-Client-IP");
			}
			return remoteAddr != null ? remoteAddr : getRequest().getRemoteAddr();
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 获取header token
	 *
	 * @return
	 */
	public static String getToken() {
		String token = getRequest().getHeader("token");
		return token;
	}

	/**
	 * 获取header client
	 *
	 * @return
	 */
	public static String getClient() {
		String client = getRequest().getHeader("client");
		return client;
	}



	/**
	 * 获取uId
	 *
	 * @return
	 */
	public static Long getLoginId() {
		String token = getToken();
		if (StringUtils.isEmpty(token)) {
			throw new RuntimeException("获取用户id error: token不能为空");
		}
		String username = JWTUtil.getUsername(token);
		String[] fields = username.split(":");
		Long uid = Long.parseLong(fields[2]);
		return uid;
	}

	/**
	 * 获取UserName
	 *
	 * @return
	 */
	public static String getUserName() {
		String token = getToken();
		if (StringUtils.isEmpty(token)) {
			throw new RuntimeException("获取用户id error: token不能为空");
		}
		String username = JWTUtil.getUsername(token);
		String[] fields = username.split(":");

		return fields[0];
	}
}
